﻿using System;
using System.Collections.Generic;
using OpenTK;
using System.Text;
using Xeno.Core;

namespace Xeno.GUI
{
    public class XuiPropGrid : XuiWidget
    {
        public bool NeedSync = false;
        public XuiPropGrid(Vector2 size,XenoNode attached)
            :base(size)
        {
            Attached = attached;
        }
        public XenoNode LastNode { get; set; }
        public override void DoUpdate()
        {
            if (UserNode != LastNode)
            {
                LastNode = UserNode;
                NeedSync = true;
            }
            if (NeedSync)
            {
                Attached.Nodes.Clear();
                ProcessNode();
                NeedSync = false;
            }
            base.DoUpdate();
        }
        public void ProcessNode()
        {
       
            float fy = 0;
            foreach (XenoModule mod in UserNode.Modules)
            {
                int cc = 1;
                Type typ = mod.GetType();
                System.Reflection.PropertyInfo[] props =  typ.GetProperties();
               
                cc = props.Length;

                XenoNode frame = XenoFactory.Xui.Group(mod.Name, this.Attached, mod.Name + " Properties.", new Vector2(5, fy), new Vector2(Size.X - 10, cc * 45 + 20));
                frame.Widget.CullNodes = false;
                float py = 25;
                foreach (var p in props)
                {
                    object[] prop = p.GetCustomAttributes(typeof(XenoProp), true);


                    if (prop.Length > 0)
                    {
                        XenoNode editNode = null;
                        XenoNode but = XenoFactory.Xui.Button("Prop_" + p.Name, frame, p.Name, new Vector2(5, py), new Vector2(100, 35));
                        XenoProp xp = prop[0] as XenoProp;
                        if (xp.Type == typeof(string))
                        {
                            editNode = XenoFactory.Xui.TextField("Prop_" + p.Name + "_Edit", frame, "Enter " + p.Name, new Vector2(110, py+7.5f), new Vector2(150, 20));

                        }
                        py = py + 45;

                    }
                    
                }
                fy = fy + frame.Widget.Size.Y+5;
            }
        }
        public override void DoRender()
        {
               
            base.DoRender();
        }
    }
}
